﻿// Inner Fire 游戏引擎库
// TreeSprite - 树木公告牌
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 所属层级：元素层（3）
// 使用公告牌技术进行树木的绘制。
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-01-13

#include "tree_sprite.h"

namespace ifire::dx {

TreeSprite::TreeSprite() {}

void TreeSprite::BuildMesh(
    ID3D12Device* device, ID3D12GraphicsCommandList* cmd_list) {
  // 很简单的按序号构架项目
  eastl::vector<std::uint16_t> indices(vertices_.size());
  for (int i = 0; i < indices.size(); i++) {
    indices[i] = i;
  }

  const UINT vbByteSize = (UINT)vertices_.size() * sizeof(TreeSpriteVertex);
  const UINT ibByteSize = (UINT)indices.size() * sizeof(std::uint16_t);

  mesh_ = std::make_unique<DxMesh>(0, "TreeSprites");
  dx::DxMeshCreateDesc desc{};
  desc.pVertexData = vertices_.data();
  desc.pIndexData = indices.data();
  desc.VertexSize = vertices_.size();
  desc.VertexStrideSize = sizeof(dx::TreeSpriteVertex);
  desc.IndexSize = indices.size();
  desc.IndexFormat = DXGI_FORMAT_R16_UINT;
  desc.IndexStrideSize = sizeof(std::uint16_t);
  mesh_->CreateResource(device, cmd_list, desc);
  mesh_->AddSubmesh("Default", 0, 0, static_cast<UINT>(desc.IndexSize), 0, 0);
}

void TreeSprite::Add(DirectX::XMFLOAT3 pos, DirectX::XMFLOAT2 size) {
  vertices_.emplace_back(pos, size);
}

} // namespace ifire::dx